//C14:14.16:func_ptr.c -- pointer to function.
#include <stdio.h>
#include <string.h>
#include <ctype.h>

char showmenu(void);
void eatline(void);    // read to line .
void show(void(* fp)(char *), char *str);
void ToUpper(char *);
void ToLower(char *);
void Transpose(char *);  // Uppper to lower, or lower to upper.
void Dummy(char *);    // no change.

int main(void)
{
	char line[8];
	char copy[8];
	char choice;
	void (*pfun)(char *);

	puts("Enter a string(empty line to quit): ");
	while (gets(line) != NULL && line[0] != '\0')
	{
		while ((choice = showmenu()) != 'n')
		{

			switch(choice)
			{
				case 'u': pfun = ToUpper;
						  break;
				case 'l': pfun = ToLower;
						  break;
				case 't': pfun = Transpose;
						  break;
				case 'o': pfun = Dummy;
						  break;
			}
		
			strcpy(copy, line);
			show(pfun, copy);
		}
		puts("Enter a string (empty line to quit): ");
	}
	puts("Bye!\n");
}

char showmenu(void)
{
	char ans;
	puts("Enter menu choice: ");
	puts("u) uppercase l) lowercase");
	puts("t) transposed case o) original case");
	puts("n) next string");

	ans = getchar();
	ans = tolower(ans);
	eatline();

	while (strchr("ulton", ans) == NULL)
	{	
		puts("Please enter a u, l, t, o, or n: ");
		ans = tolower(getchar());
		eatline();
	}

	return ans;
}

void eatline(void)
{
	while (getchar() != '\n')
	{
		continue;
	}
}

void ToUpper(char *str)
{
	while (*str)
	{
		*str = toupper(*str);
		str++;
	}
}

void ToLower(char *str)
{
	while (*str)
	{
		*str = tolower(*str);
		str++;
	}
}

void Transpose(char *str)
{

	while (*str)
	{
		if(islower(*str))
		{
			*str = toupper(*str);
		}
		else if(isupper(*str))
		{
			*str = tolower(*str);
		}
		str++;
	}
}

void Dummy(char *str)
{
	
}

void show(void (* fp)(char *), char *str)
{
	(*fp)(str);
	puts(str);
}
